/**
 * commentController
 */
const _ = require('lodash')
const { nanoid } = require('nanoid')
const dateFormat = require('../utils/date')
const globalSendResult = require('../utils/globalSendResult')
const CommentsServices = require('../services/CommentsServices')
const { getCurrentUserData } = require('../utils/authorization')
const CommentsController = {
    addComment: (req,res) => {
        const commentData = _.clone(req.body)
        const { username,email, userid } = getCurrentUserData(req)
        const formatData = {
            comment_date: dateFormat(Date.now(),'YYYY-MM-DD HH:mm:ss'),
            comment_id: nanoid(),
            comment_status: 1,
            comment_user_id: userid,
            comment_user_name: username,
            comment_user_email: email
        }
        globalSendResult({
            promiseFunc: CommentsServices.add(Object.assign(commentData,formatData)),
            response: res,
            errMsg: '新增失败',
            msg: '新增',
            isReturn: false
        })
    },
    deleteOne: (req,res) => {
        const { comment_id } = req.body
        globalSendResult({
            promiseFunc: CommentsServices.deleteOne(comment_id),
            response: res,
            errMsg: '删除失败',
            msg: '删除',
            isReturn: false
        })
    },
    shield: (req,res) => {
        const { comment_id, comment_status } = req.body
        globalSendResult({
            promiseFunc: CommentsServices.shield(comment_id, comment_status),
            response: res,
            errMsg: '屏蔽失败',
            msg: '屏蔽',
            isReturn: false
        })
    },
    query: (req,res) => {
        const { comment_news_id, comment_status,  comment_content, page = 1, limit = 12 } = req.query
        const queryData = {
            comment_news_id,
            comment_content,
            page,
            limit
        }
        if(comment_status) queryData.comment_status = comment_status
        globalSendResult({
            promiseFunc: CommentsServices.query(queryData),
            response: res,
            errMsg: '查询失败',
            msg: '查询',
            countTotal: true,
            queryData,
            Services: CommentsServices,
        })
    }
}

module.exports = CommentsController
